package bbalgae.swing.outline;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/*
 * 고급 컴포넌트(많은 데이터 표시)
 * JList : 몇가지 데이터 목록을 나란히 표시하고 한가지를 선택하게 함
 * JComboBox : 일명 dropdwonlist라고 함 평소에는 전체 목록을 숨기고 있다가 클릭하면 전체 목록이 나타남 
 * 						최종적으로 선택한 항목이 화면에 표시됨
 * JTable : 엑셀처럼 대량의 데이터들을 표 형 태로 나타냄
 */
public class J21_AdvancedComponent extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					J21_AdvancedComponent frame = new J21_AdvancedComponent();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public J21_AdvancedComponent() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		String[] datalist = { "김태희", "송혜교", "전지현", "한효주" };

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(150, 50));
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		panel.add(scrollPane);
	
		final JList<String> list = new JList<>();
		scrollPane.setViewportView(list);
		list.setSize(new Dimension(50, 50));
		list.setListData(datalist);

		JButton btn1 = new JButton("New button");
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int idx = list.getSelectedIndex();
				String val = list.getSelectedValue();
				String msg = idx + "/" + val;
				JOptionPane.showMessageDialog(null, msg);
			}
		});

		JButton btnNewButton = new JButton("New button");
		panel.add(btnNewButton);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);

		JComboBox<String> comboBox = new JComboBox<>();
		panel_1.add(comboBox);

		JButton btn2 = new JButton("New button");
		panel_1.add(btn2);

		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.WEST);

		JPanel panel_3 = new JPanel();
		contentPane.add(panel_3, BorderLayout.EAST);
		
		JPanel panel_4 = new JPanel();
		contentPane.add(panel_4, BorderLayout.CENTER);
		
		
		
		//table = new JTable(8,5); //8행 5열 테이블 
		//	테이블의 제목행 생성
		String[] tbtitle = {"번호","이름","국어","영어","수학"};
			
		//테이블의 본문행 생성
		String[][] tbdata ={ {"1", "김태희","99","89","99" },
				{"2", "한효주","100","100","100" },
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
				{"3", "현아","99","59","99" },				
		};
		//제목/본문행을 DefaultTableModel 클래스에 등록 
		DefaultTableModel tbmodel = new DefaultTableModel(tbdata,tbtitle);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_1.setPreferredSize(new Dimension(350, 100));
		panel_4.add(scrollPane_1);
				
		//JTable 생성자에 DefaultTableModel 클래스 전달
		table = new JTable(tbmodel);
		scrollPane_1.setViewportView(table);
		
		
	}

}
